历史上,C++ 缺乏与硬件服务进行统一交互的方式,迫使开发者陷入 “平台孤岛” 代码库因操作系统特定的 API(如 Win32 或 POSIX)而被割裂。本页标志着向一个新时代的转变,在这个时代中,C++ 标准库充当了通用抽象层的角色。
1. #ifdef 乱码时代的终结
在标准化之前,像创建线程或遍历目录这样的简单任务需要使用预处理器宏来处理不同的系统头文件(例如, <windows.h> 与 <pthread.h>)。这导致了臃肿且难以维护的代码。
2. C++11 的范式转变
标准开始重新掌控系统资源。具体而言, C++11 引入了高层并发特性,包括 std::thread、std::mutex 和 std::future,从而统一了语言与 CPU 之间的关系。
3. 解耦厂商逻辑
通过摆脱平台特定代码,标准库提供了“一次编写,随处编译”的保障。平台维护的责任从开发者转移到了编译器供应商身上。
main.py
TERMINALbash — 80x24
> Ready. Click "Run" to execute.
>
QUESTION 1
Which C++ standard first introduced a unified concurrency model into the Standard Library?
C++98
C++11
C++14
C++17
✅ Correct!
C++11 added std::thread, std::mutex, and the memory model required for standardized concurrency.❌ Incorrect
Prior to C++11, concurrency was handled by external libraries or platform APIs like POSIX.QUESTION 2
What is the primary disadvantage of using '#ifdef _WIN32' for threading logic?
It makes code non-portable and difficult to maintain.
It runs slower than std::thread.
It prevents the use of templates.
It requires a specific commercial license.
✅ Correct!
Platform-specific macros create 'spaghetti' code that must be rewritten for every new operating system.❌ Incorrect
While it might be fast, the main issue is the lack of portability and the fragmented logic.QUESTION 3
Which utility added in C++11 is used to manage a value that will be available later?
std::vector
std::mutex
std::future
std::chrono
✅ Correct!
std::future provides a mechanism to access the result of an asynchronous operation.❌ Incorrect
std::mutex is for mutual exclusion; std::future is for asynchronous results.QUESTION 4
Who is responsible for implementing the OS-specific logic behind 'std::thread'?
The application programmer
The hardware manufacturer
The compiler/standard library vendor
The user at runtime
✅ Correct!
The compiler vendor (like GCC, Clang, or MSVC) maps standard calls to the target OS kernel calls.❌ Incorrect
The abstraction layer's purpose is to remove this responsibility from the application developer.QUESTION 5
What does the term 'Platform Silo' refer to in this context?
A secure storage area for source code.
Code that can only run on one specific OS due to API dependencies.
A high-performance server farm.
A design pattern for database isolation.
✅ Correct!
Silos occur when code is locked into proprietary APIs (e.g., using Win32 API calls directly).❌ Incorrect
It refers to architectural isolation and lack of portability, not literal storage.Case Study: Modernizing a Legacy Downloader
Transitioning from POSIX to Modern C++ Standard Library
A developer in 2005 wrote a multi-threaded downloader using 'CreateThread' for Windows and 'pthread_create' for Linux. The codebase is 40% preprocessor macros.
Q
1. What single C++11 class should replace both CreateThread and pthread_create?
Solution:
The developer should use
The developer should use
std::thread. This standardizes the execution model and removes the need for platform-specific headers.Q
2. If the downloader needs to return the size of the downloaded file asynchronously, which concurrency feature is most appropriate?
Solution:
std::future combined with std::async (or a promise) is best for retrieving return values from background tasks safely and cleanly.Q
3. What is the 'Write Once, Compile Anywhere' benefit in this scenario?
Solution:
It means the same threading source code can be compiled for Windows, Linux, or macOS without any changes, as the Standard Library handles the OS translation.
It means the same threading source code can be compiled for Windows, Linux, or macOS without any changes, as the Standard Library handles the OS translation.